package megrez.mail.sender.component.template;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import de.neuland.pug4j.Pug4J;
import de.neuland.pug4j.PugConfiguration;
import de.neuland.pug4j.exceptions.PugException;
import de.neuland.pug4j.template.PugTemplate;
import megrez.mail.sender.common.MailSenderException;

/**
 * Jade模板处理
 * 
 * @author Lucky Yang
 * @since 2.6.9
 */
@Component
public class Pug4jTemplageProcess implements TemplateProcess {
    @Autowired
    private PugConfiguration configuration;

    @Override
    public String render(String templateValue, Map<String, Object> params) {
        try {
            PugTemplate template = configuration.getTemplate(templateValue);
            Writer writer = new StringWriter();
            Pug4J.render(template, params, writer);
            return writer.toString();
        } catch (PugException | IOException e) {
            throw new MailSenderException(e);
        }
    }

}
